﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;


using $prjsn$.Entity.Services.HowTos;
using $prjsn$.Common;
using $prjsn$.Common.Data.Services.HowTos;
using $prjsn$.Common.Business.Services.HowTos;

namespace $safeprojectname$.Services.HowTos
{
    /// <summary>
    /// All Business checks for 'SimpleEntity' on CRUD operations
    /// are nested and encapsulated in this SimpleEntityFacade
    /// </summary>
    [CLSCompliant(false)]
    public class SimpleEntityFacade : BaseFacade<SimpleEntity, string, SimpleEntityFilter, ISimpleEntityDao>, ISimpleEntityFacade
    {
        #region IBaseFacade<T> override - FACTORY METHOD
        /// <summary>
        /// Factory Method pattern.
        /// A central and only point for creating new entity of the type SimpleEntity.
        /// </summary>
        /// <returns>new entity 'SimpleEntity' transient</returns>
        public override SimpleEntity CreateNew()
        {
            return new SimpleEntity
            {
                // TODO
            };
        }
        /// <summary>
        /// Needed for the BaseFacade checks on Update() and Delete().
        /// </summary>
        /// <returns></returns>
        public override SimpleEntityFilter CreateNewFilter()
        {
            return new SimpleEntityFilter
            {
                // TODO
            };
        }
        #endregion IBaseFacade<T> override - FACTORY METHOD

        #region override Update
        public override SimpleEntity Update(SimpleEntity entity)
        {
            var result = base.Update(entity);
            // Async ADD
            if (entity.NewComplexAsync.IsNotEmpty())
            {
                foreach (var item in entity.NewComplexAsync)
                {
                    item.SimpleAsync = entity;
                    ComplexEntityFacade.Update(item);
                }
            }
            // Auto ADD
            if (entity.NewComplexAuto.IsNotEmpty())
            {
                foreach (var item in entity.NewComplexAuto)
                {
                    item.SimpleAuto = entity;
                    ComplexEntityFacade.Update(item);
                }
            }
            // Auto REMOVE
            if (entity.RemovedComplexAuto.IsNotEmpty())
            {
                foreach (var item in entity.RemovedComplexAuto.Distinct())
                {
                    item.SimpleAuto = null;
                    ComplexEntityFacade.Update(item);
                }
            }
            return result;
        }

        #endregion override Update

        #region ISimpleEntityFacade
        // TODO any application dependent operations,
        // declared in ISimpleEntityFacade
        // put here (e.g. 'public virtual bool IsOtherEntityInUse(OtherEntity item)')
        #endregion ISimpleEntityFacade
        
        #region properties

        // NOTE: You MUST extend ISimpleEntityFacade declaration this way:
        //  IComplexEntityFacade ComplexEntityFacade { set; }

        public virtual IComplexEntityFacade ComplexEntityFacade { protected get; set; }

        #endregion properties
    }
}
